# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libssh

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.6.3
pkgrel=1
pkgdesc="Library for accessing ssh client services through C libraries (mingw-w64)"
arch=('any')
url="http://www.libssh.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-openssl")
groups=("${MINGW_PACKAGE_PREFIX}")
options=('staticlibs' 'strip')
source=("https://red.libssh.org/attachments/download/87/${_realname}-0.6.3.tar.xz"
    mingw-w64-pthreads.patch
    mingw-w64-paths-as-unix.patch)
md5sums=('66cf16e77f60913b4d54f18c92cdbf71'
         'ca268e1366633f988e21cbcf5a4d8145'
         '53fd88870e2ad09d2e69b34c839b1c8c')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/mingw-w64-pthreads.patch
  #patch -p1 -i ${srcdir}/mingw-w64-paths-as-unix.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${PREFIX_WIN} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${pkgver}
  make all
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install
  
  sed -e "s|-Iinclude|-I${MINGW_PREFIX}/include|g" \
    -e "s|-Llib|-L${MINGW_PREFIX}/lib|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libssh.pc \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libssh_threads.pc
  rm -rf ${pkgdir}/${MINGW_PREFIX}/CMake
}
